//
//  SHBadgeButton.m
//  ShinyOrder
//
//  Created by 王方伟 on 16/7/22.
//  Copyright © 2016年 ShinyHub. All rights reserved.
//

#import "SHBadgeButton.h"
#import "NSString+Extension.h"
#import "UIView+Extension.h"
#import "SHManHeader.h"

#define KDistance 60.0

@interface SHBadgeButton ()
@property (assign, nonatomic) CGFloat smalRadius;
@property (copy, nonatomic) FinishBlock comBlock;
@end
@implementation SHBadgeButton

- (instancetype)initWithFrame:(CGRect)frame{
    self = [super initWithFrame:frame];
    if (self) {
        [self initBadgeButton];
    }
    return self;
}

- (instancetype)initWithCoder:(NSCoder *)coder{
    self = [super initWithCoder:coder];
    if (self) {
        [self initBadgeButton];
    }
    return self;
}

- (void)badgeValue:(NSString *)badgeValue finishBlock:(FinishBlock)block{
    _comBlock = block;
    self.badgeValue = badgeValue;
}

-(void)setBadgeValue:(NSString *)badgeValue{
    _badgeValue=[badgeValue copy];
    //设置提示数字
    if (badgeValue.integerValue != 0 && badgeValue.length > 0) {
        self.hidden=NO;
        self.titleLabel.text = badgeValue;
        if (badgeValue.integerValue == -1) {
            [self setTitle:@"" forState:UIControlStateNormal];
            self.width = 9;
            self.height = 9;
            return;
        }else{
            [self setTitle:badgeValue forState:UIControlStateNormal];
        }
        CGFloat width = [badgeValue sizeWithFont:self.titleLabel.font maxSize:CGSizeMake(MAXFLOAT, MAXFLOAT)].width;
        if (width > 8.0 && width < 24) {
            self.width = width + 6;
            self.height = 18;
        }else if (width > 24) {
            self.width = width + 24;
            self.height = 22;
        }else{
            self.width = 18;
            self.height = 18;
        }
    }else{
        self.hidden=YES;
    }
}

- (void)setFrame:(CGRect)frame{
    self.layer.cornerRadius = frame.size.height * 0.5;
    [super setFrame:frame];
}

-(void)initBadgeButton{
    self.hidden=YES;
    self.backgroundColor = SHColor(254, 56, 36);
    self.titleLabel.font = [UIFont systemFontOfSize:12];
    self.size = CGSizeMake(LineX(18), LineY(18));
}
@end
